<?php
class Team_log extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        // $this->load->helper('url');
        // 登录校验
        $this->common->check_login_mobile();
        // 通过user_model的get_user_info_id方法获取个人信息
        $this->user_info = $this->user->get_user_info_id($_SESSION ['mobile'] ['id']);

        $this->load->helper(array(
            'form',
            'url',
            'system'
        ));


        $this->load->model('team_model');

        $this->load->model('order_model');
    }

    /**
     * 团队合作记录
     */
    public function index($page = 0)
    {
        $perpage = 10;
        if ($page == 0) {
            $page = 1;
        }

        $data['left'] = $this->load->view('mobile/left', '', TRUE);
        $data['foot'] = $this->load->view('mobile/foot', '', TRUE);

        //var_dump($id_str);
        $data['data_list'] = $this->order_model->get_up_account_offer_list($this->user_info['team_lv'], $page, $perpage, $this->user_info['path']);//$this->team_model->team_list_all($page,$perpage,$_SESSION['user']['id']);

        $data['data_count'] = $this->order_model->get_up_account_offer_list_num($this->user_info['team_lv'], $this->user_info['path']);
        //var_dump($data['data_count']);
        $data['multi'] = multi($data['data_count'], $perpage, $page, site_url('mobile/team_log/index/?'), '?');

        $data['user'] = $this->user_info;

        $this->load->view('mobile/team_log', $data, FALSE);
    }

    /**
     * 是否接单
     */
    public function info_post($type,$offer_id,$user_id,$money)
    {
        $this->load->model('order_model');

        if(empty($offer_id) || empty($user_id)|| empty($money)){
            exit($this->common->alert_msg_goback('参数错误'));
        }

        if($user_id != $this->user_info['id'])
        {
            exit($this->common->alert_msg_goback('非法操作'));
        }

        //接单
        if($type > 0)
        {
            //更新旧订单信息
            $this->order_model->offer_order_on_off_userid($offer_id,$user_id);

            $bianhao = $this->common->getnumChar ( 7 );

            $has_order = $this->order_model->offer_order_has($user_id,$offer_id);

            if(empty($has_order['num']))
            {
                //插入offer表已匹配
                $this->order_model->offer_order_new_insert($user_id,$money,$bianhao,$offer_id);
                //新offerid
                $insert_id = $this->db->insert_id();
                //根据旧offer_id查询作废日志，生成新日志
                $log_list = $this->order_model->order_log_list(0,$offer_id);
                //var_dump($log_list);
                //循环插入order_log，插入的新offer的匹配记录
                foreach ($log_list as $k => $v) {
                    $this->order_model->order_log_insert($insert_id,$v['accept_order_id'],$v['money'],$bianhao);
                }
            }

            exit($this->common->alert_msg_url('成功',site_url('offer_help')));

        }
        else
        {
            //更新旧订单信息
            $this->order_model->offer_order_on_off_userid($offer_id,-1);
            //拒绝减20%
            $new_money = $money*0.2;
            //更新
            $this->user->update_minus_user_money(0,$new_money,$user_id);
            //更新接受帮助订单金额
            $log_list = $this->order_model->order_log_list(0,$offer_id);

            foreach ($log_list as $k => $v) {
                //$this->order_model->order_log_insert($insert_id,$v['accept_order_id'],$v['money'],$bianhao);
                $this->order_model->update_accept_money($v['accept_order_id'],$v['money']);
            }

            //扣款
            exit($this->common->alert_msg_goback('静态金额扣款'.$new_money));

        }
    }
}